import torch
import torch.nn as nn


class MnistModel(nn.Module):
    def __init__(self):
        super(MnistModel, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)  # 输入通道1，输出通道32
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
        self.pool = nn.MaxPool2d(2, 2)  # 池化层
        self.fc1 = nn.Linear(64 * 7 * 7, 128)  # 全连接层
        self.fc2 = nn.Linear(128, 10)  # 输出10类（0-9）

    def forward(self, x):
        x = torch.relu(self.conv1(x))  # [B, 32, 28, 28]
        x = self.pool(x)  # [B, 32, 14, 14]
        x = torch.relu(self.conv2(x))  # [B, 64, 14, 14]
        x = self.pool(x)  # [B, 64, 7, 7]
        x = x.view(-1, 64 * 7 * 7)  # 展平
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x
